package com.leilei.dao.impl;

import com.leilei.dao.IShopCartDao;
import com.leilei.entity.Address;
import com.leilei.entity.ShoppingCart;
import com.leilei.entity.ShoppingCustom;
import com.leilei.util.DBUtil;
import com.leilei.util.DateUtil;
import org.apache.commons.dbutils.QueryRunner;

import java.sql.SQLException;
import java.util.Date;
import java.util.Map;

public class ShopCartDaoImpl implements IShopCartDao {

    private QueryRunner queryRunner = new QueryRunner(DBUtil.getDataSource());

    @Override
    public int saveShopCart(Map<ShoppingCustom, ShoppingCart> shopCartMap) {

        int count = 0;

        for (ShoppingCustom shoppingCustom : shopCartMap.keySet()) {

            ShoppingCart shoppingCart = shopCartMap.get(shoppingCustom);

            String sql = "insert into shopcustom(uid, pid, pcid, pvid, buycount, buytime, isbuy)" +
                    "values(?, ?, ?, ?, ?, ?, ?)";

            //查找送货地址信息
//            Address address = new Address();
//            for (Address addr : shoppingCart.getUser().getAddressList()) {
//                address.setDid(addr.getDid());
//                address.setAddress(addr.getAddress());
//                address.setTel(addr.getTel());
//            }

            try {
                int result = queryRunner.update(sql, shoppingCart.getUser().getUid(),
                        shoppingCustom.getPid(),
                        shoppingCustom.getPcid(),
                        shoppingCustom.getPvid(),
                        shoppingCart.getCount(),
                        DateUtil.utilToSql(new Date()),
                        shoppingCart.getIsbuy()
                        );
                if (result > 0){
                    count++;
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        return count;
    }

    @Override
    public Map<ShoppingCustom, ShoppingCart> selectCartNoBuyByUid(int uid) {
        return null;
    }

    @Override
    public Map<ShoppingCustom, ShoppingCart> selectCartBuyByUid(int uid) {
        return null;
    }

}
